// components/Item-line/Item-line.js
const collect = require("../../models/collect");
const history = require("../../models/history");
const paid = require("../../models/paid");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value:{
      type:Object
    },
    fct:{
      type:String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    async onClose(event) {
      const { position, instance } = event.detail;
      switch (position) {
        case 'left':
        case 'cell':
          instance.close();
          break;
        case 'right':
          var strongID = wx.getStorageSync('id');
          // 登录了就实现收藏或取消收藏功能
          if (strongID) {
            if(this.data.fct=="collect"){
              // 收藏true为取消收藏
              var res = await collect(strongID, this.data.value._id, true);
            }else if(this.data.fct=="history"){
              // 历史记录false为删除历史记录
              var res = await history(strongID,this.data.value._id,false)
            }else if(this.data.fct=="paid"){
              // 历史记录false为删除历史记录
              var res = await paid(strongID,this.data.value._id,false)
            }
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          } else {
            wx.navigateTo({
              url: '../login/login'
            });
          }
          instance.close();
          break;
      }
    },
    handleClick() {
      // 传递删除的商品的id
      let id = this.properties.value._id;
      this.triggerEvent("Id",id)
    }
  }
})